package com.springboot.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.task.TaskSchedulingProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务配置类
 *
 * @author yexi
 * @see TaskSchedulingProperties
 * @see ScheduledAnnotationBeanPostProcessor#processScheduled(Scheduled, Method, Object)
 * @see ScheduledTaskRegistrar#scheduleTasks()
 * @see org.springframework.scheduling.concurrent.ReschedulingRunnable#schedule()
 */
@Configuration
public class TimingTask {

    private static final Log log = LogFactory.getLog(TimingTask.class);

    @Scheduled(initialDelay = 0, fixedDelay = 1000)
    public void task1() throws InterruptedException {
        log.info("定时任务1开始执行，当前时间为: " + LocalDateTime.now());
        //模拟定时任务处理花费的时间
        TimeUnit.SECONDS.sleep(10);
        log.info("定时任务1执行结束，当前时间为: " + LocalDateTime.now());
    }

    @Scheduled(cron = "0/3 * * * * ?")
    public void task2() throws InterruptedException {
        log.info("定时任务2开始执行，当前时间为: " + LocalDateTime.now());
        //模拟定时任务处理花费的时间
        TimeUnit.SECONDS.sleep(5);
        log.info("定时任务2执行结束，当前时间为: " + LocalDateTime.now());
    }


}
